package com.sudo.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.mapper.my.SysUserBehaviorMapper;
import com.sudo.pojo.SysUserBehavior;
import com.sudo.service.BaseService;
import com.sudo.service.SysUserBehaviorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author：为道日损 2020-04-24 13:04
 */
@Service
public class SysUserBehaviorServiceImpl extends BaseService implements SysUserBehaviorService {

    @Autowired
    private SysUserBehaviorMapper sysUserBehaviorMapper;

    /**
     * 向数据库中增加用户行为日志
     * @param sysUserBehavior 日志对象
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void addSysUserBehavior(SysUserBehavior sysUserBehavior) {
        sysUserBehaviorMapper.insert(sysUserBehavior);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public PagedGridResult queryUserBehaviorByPage(Integer page, Integer pageSize, String username ,String startDate ,String endDate) {
        PageHelper.startPage(page, pageSize);
        Example example = new Example(SysUserBehavior.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(username)) {
            criteria.andEqualTo("username",username);
        } else if (StringUtils.isNotBlank(startDate)) {
            criteria.andGreaterThanOrEqualTo("operTime",startDate);
        } else if (StringUtils.isNotBlank(endDate)) {
            criteria.andLessThanOrEqualTo("operTime",endDate);
        }
        List<SysUserBehavior> sysUserBehaviorList = sysUserBehaviorMapper.selectByExample(example);
        return setterPagedGrid(sysUserBehaviorList, page);
    }
}
